var app = getApp();
// pages/login/renzheng.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    real_name: '',
    idcard: '',
    //=========
    files: [],
    uploadimg: [],
    files_up: [],
    uploadimg_thumb: '',
    uploadimg_thumb_fan: '',
    checked:false,
    zheng: '/images/id2.png',
    fan: '/images/id1.png',
    name:'',
    idcard:'',
    button_text: '立即认证',
    visible1: false,
    checkbox: [],
    
    actions: [
      {
        name: '取消',
      },
      {
        name: '认证',
        color: '#FE7E38'
      },
    ],
    userinfo: {}

    //=======
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let userinfo = app.http.getUserInfo();
      this.setData({
        userinfo : userinfo
      });
      if(userinfo.identity_card_front) {
        this.setData({
          button_text: '下一步',
          name: userinfo.real_name,
          idcard: userinfo.identity_card,
          zheng: userinfo.identity_card_front,
          fan: userinfo.identity_card_back
        });
      }
      
  },


  
  checkboxChange(e) {
    let arr = e.detail.value;
    this.setData({
      checkbox: arr
    });
  },
  
  submit:function(e){
   
  },
 
  chooseImage: function (e) {        console.log('ada1111111dad');

    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log('adadad');
        console.log(res);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var files = that.data.files.concat(res.tempFilePaths)
     
        that.uploadimage(files[0],'zheng');
 
      }
    })
  },
  chooseImage_fan: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
      
        that.uploadimage(res.tempFilePaths[0],'fan');

      }
    })
  },
  

  //上传图片
  uploadimage: function (t,v) {
    var that = this;
    var userinfo = app.http.getUserInfo();
    var timestamp = new Date().getTime();
    wx.uploadFile({
      url: app.http.apiurl + 'common/upload',
      filePath: t,
      name: 'file',
      formData: {
        token: userinfo.token
      },
      success: function (res) {
        var data = JSON.parse(res.data)
        //console.log(data)
        if(v=='zheng'){
          that.setData({
            zheng: data.data.fullurl,
            uploadimg_thumb: data.data.url
          })
        }else{
          that.setData({
            fan: data.data.fullurl,
            uploadimg_thumb_fan: data.data.url
          })
        }
        
      }
    })
  },
  torenzheng(e) {
    if (this.data.button_text === '下一步') {
      return wx.navigateTo({
        url: '/pages/v4_sass/login/bind-bankcard'
      });
      return;
    }
    var that = this;
    var uploadimg_thumb = that.data.uploadimg_thumb;
    var uploadimg_thumb_fan = that.data.uploadimg_thumb_fan;

    if (uploadimg_thumb == '') {
      wx.showToast({
        title: '请上传身份证正面照片',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if (uploadimg_thumb_fan == '') {
      wx.showToast({
        title: '请上传身份证反面照片',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }

    app.http.$ajax(
      {
        url: 'v1.user/certification',
        data: {
          identity_card_front: uploadimg_thumb,
          identity_card_back: uploadimg_thumb_fan
        },
        loading: '认证中...'
      },
      {
        success(res) {
          app.http.saveUser( res.data)

          wx.showToast({
            title: '认证成功',
            duration: 2000,
            icon: 'none',
          })
          that.setData({
            button_text: '下一步',
            name: res.data.real_name,
            idcard: res.data.identity_card
          });
         
        }
      }
    ) 
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})